package com.yanfan.zutai.controller;

import com.yanfan.common.annotation.Log;
import com.yanfan.common.core.controller.BaseController;
import com.yanfan.common.core.domain.AjaxResult;
import com.yanfan.common.core.page.TableDataInfo;
import com.yanfan.common.enums.BusinessType;
import com.yanfan.common.utils.poi.ExcelUtil;
import com.yanfan.zutai.aop.SysInitData;
import com.yanfan.zutai.aop.SysInitUpdateData;
import com.yanfan.zutai.aop.SysQueryData;
import com.yanfan.zutai.domain.SysEcharts;
import com.yanfan.zutai.service.ISysEchartsService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.jdbc.core.JdbcTemplate;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletResponse;
import java.util.List;

/**
 * echats示例中心Controller
 *
 * @author ruoyi
 * @date 2023-06-02
 */
@RestController
@RequestMapping("/zutai/sysEcharts")
public class SysEchartsController extends BaseController {
    @Autowired
    private ISysEchartsService sysEchartsService;
    @Autowired
    private JdbcTemplate jdbcTemplate;

    /**
     * 查询echats示例中心列表
     */
    //@PreAuthorize("@ss.hasPermi('ghxx:sysEcharts:list')")
    @GetMapping("/list")
    @SysQueryData
    public TableDataInfo list(SysEcharts sysEcharts) {
        startPage();
        List<SysEcharts> list = sysEchartsService.selectSysEchartsList(sysEcharts);
        return getDataTable(list);
    }

    /**
     * 导出echats示例中心列表
     */
    //@PreAuthorize("@ss.hasPermi('ghxx:sysEcharts:export')")
    @Log(title = "echats示例中心", businessType = BusinessType.EXPORT)
    @PostMapping("/export")
    public void export(HttpServletResponse response, SysEcharts sysEcharts) {
        List<SysEcharts> list = sysEchartsService.selectSysEchartsList(sysEcharts);
        ExcelUtil<SysEcharts> util = new ExcelUtil<SysEcharts>(SysEcharts.class);
        util.exportExcel(response, list, "echats示例中心数据");
    }

    /**
     * 获取echats示例中心详细信息
     */
    //@PreAuthorize("@ss.hasPermi('ghxx:sysEcharts:query')")
    @GetMapping(value = "/{id}")
    public AjaxResult getInfo(@PathVariable("id") Long id) {
        return AjaxResult.success(sysEchartsService.selectSysEchartsById(id));
    }

    /**
     * 新增echats示例中心
     */
    //@PreAuthorize("@ss.hasPermi('ghxx:sysEcharts:add')")
    @Log(title = "echats示例中心", businessType = BusinessType.INSERT)
    @PostMapping
    @SysInitData
    public AjaxResult add(@RequestBody SysEcharts sysEcharts) {
        return toAjax(sysEchartsService.insertSysEcharts(sysEcharts));
    }

    /**
     * 修改echats示例中心
     */
    //@PreAuthorize("@ss.hasPermi('ghxx:sysEcharts:edit')")
    @Log(title = "echats示例中心", businessType = BusinessType.UPDATE)
    @PutMapping
    @SysInitUpdateData
    public AjaxResult edit(@RequestBody SysEcharts sysEcharts) {
        return toAjax(sysEchartsService.updateSysEcharts(sysEcharts));
    }

    /**
     * 删除echats示例中心
     */
    //@PreAuthorize("@ss.hasPermi('ghxx:sysEcharts:remove')")
    @Log(title = "echats示例中心", businessType = BusinessType.DELETE)
    @DeleteMapping("/{ids}")
    public AjaxResult remove(@PathVariable Long[] ids) {
        return toAjax(sysEchartsService.deleteSysEchartsByIds(ids));
    }
    /**
     * 同步echats示例中心，官方地址https://www.isqqw.com/
     */
//    @Log(title = "同步echats示例中心", businessType = BusinessType.INSERT)
//    @PostMapping(value = "/dataSyncSysEcharts")
//    public AjaxResult dataSyncSysEcharts() throws Exception
//    {
//        String token="eyJ0eXAiOiJKV1QiLCJhbGciOiJIUzI1NiJ9.eyJleHAiOjE2ODgyODAwODYsInVzZXJuYW1lIjoiMTk1MjIyOTA2MTQifQ.6flsFppXIwSGdqDcUbu71s4sTMmcBkNMoDAord7CIeE";
//        //212
//        for (int pageNum = 1; pageNum < 220; pageNum++) {
//            System.out.println("同步进度："+pageNum);
//            try {
//                String sql="https://api.isqqw.com/api/1.1/charts/list?searchWord=&pageNum="+pageNum+"&" +
//                        "pageSize=100&typeCode=all&viewSortStatus=2&sign=89ac5e1eb8bf37dcc16faa0e2457b71f";
//                String returnMsg = HttpPostAndGetUtil.doPost(sql, "","");
//                SysEchartsMsg sysEchartsMsg = JSON.parseObject(returnMsg, SysEchartsMsg.class);
//                SysEchartsData sysEchartsData = JSON.parseObject(sysEchartsMsg.getData().toString(), SysEchartsData.class);
//                for (int i = 0; i <sysEchartsData.getRecords().size(); i++) {
//                    SysEcharts echarts = sysEchartsData.getRecords().get(i);
//                    //因图表库升级加密，所以同步不了了，只能修改图片了
//                    String updateSql=" update sys_echarts set img=? where echarts_id=?";
//                    jdbcTemplate.update(updateSql,echarts.getImg(),echarts.getId());
////                    String detail="https://api.isqqw.com/api/1.1/charts/echartsdetail?id="+echarts.getId();
////                    SysEcharts sysEchartsQuery=new SysEcharts();
////                    sysEchartsQuery.setEchartsId(echarts.getId());
////                    List<SysEcharts> echartsList = sysEchartsService.selectSysEchartsList(sysEchartsQuery);
////                    if(echartsList.size()>0){
////                        echarts.setEchartsId(echarts.getId());
////                        echarts.setId(echartsList.get(0).getId());
////                        returnMsg = HttpPostAndGetUtil.doPost(detail, "",token);
////                        sysEchartsMsg = JSON.parseObject(returnMsg, SysEchartsMsg.class);
////                        SysEcharts echart = JSON.parseObject(sysEchartsMsg.getData().toString(), SysEcharts.class);
////                        echarts.setCode(echart.getCode());
////                        echarts.setCid(echart.getCid());
////                        sysEchartsService.updateSysEcharts(echarts);
////                    }else{
////                        echarts.setEchartsId(echarts.getId());
////                        returnMsg = HttpPostAndGetUtil.doPost(detail, "",token);
////                        sysEchartsMsg = JSON.parseObject(returnMsg, SysEchartsMsg.class);
////                        SysEcharts echart = JSON.parseObject(sysEchartsMsg.getData().toString(), SysEcharts.class);
////                        echarts.setCode(echart.getCode());
////                        echarts.setId(null);
////                        echarts.setCid(echart.getCid());
////                        sysEchartsService.insertSysEcharts(echarts);
////                    }
//                }
//            }catch (Exception e){
//                System.out.println(e.toString());
//                e.printStackTrace();
//            }
//        }
//
//        return AjaxResult.success();
//    }
}
